package com.alibaba.japan.club.runtime.springmvc.servlet.view.freemarker;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import org.springframework.context.i18n.LocaleContext;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.web.servlet.support.RequestContext;

import freemarker.template.TemplateMethodModel;
import freemarker.template.TemplateModelException;

/**
 * FreeMarker的工具类<br>
 * 该类用来定义FreeMarker的自定义函数
 * 
 * @author:shaochuan.wangsc
 * @date:2009-3-24
 * 
 */
public class FreeMarkerFunction {

	private static final String JAPAN = "JP";

	private static final String JAPAN_LANGUAGE = "ja";

	private static final String CHINA = "CN";

	private static final String CHINA_LANGUAGE = "zh";

	/**
	 * 自定义locale函数,可通过此扩展的内置函数判断当前系统语言
	 * 
	 * @author shaochuan.wangsc
	 * 
	 */
	public static class LocaleFunction implements TemplateMethodModel {
		@SuppressWarnings("unchecked")
		public Object exec(List arguments) throws TemplateModelException {
			Boolean result = Boolean.FALSE;
			if (arguments == null || arguments.isEmpty() || "".equals(arguments.get(0))) {
				return result;
			}

			String language = (String) arguments.get(0);
			LocaleContext localeContext = LocaleContextHolder.getLocaleContext();
			Locale locale = localeContext.getLocale();
			String localeLanguage = locale.getLanguage();
			if (JAPAN.equals(language) && JAPAN_LANGUAGE.equals(localeLanguage)) {
				result = Boolean.TRUE;
			}
			if (CHINA.equals(language) && CHINA_LANGUAGE.equals(localeLanguage)) {
				result = Boolean.TRUE;
			}
			return result;
		}
	}

	/**
	 * 自定义message函数,可通过此扩展的内置函数可获取message信息
	 * 
	 * @author shaochuan.wangsc
	 * 
	 */
	public static class MessageFunction implements TemplateMethodModel {
		RequestContext requestContext;

		public MessageFunction(RequestContext requestContext) {
			this.requestContext = requestContext;
		}

		@SuppressWarnings("unchecked")
		public Object exec(List arguments) throws TemplateModelException {
			String result = "";
			if (arguments == null || arguments.isEmpty() || "".equals(arguments.get(0))) {
				return result;
			}

			String firstArgument = (String) arguments.get(0);
			LocaleContext localeContext = LocaleContextHolder.getLocaleContext();
			Locale locale = localeContext.getLocale();
			if (JAPAN.equals(firstArgument) || CHINA.equals(firstArgument)) {
				List<Object> args = new ArrayList<Object>();
				String secondArgument = "";
				if (arguments.size() > 2) {
					for (int i = 2; i < arguments.size(); i++) {
						args.add(arguments.get(i));
					}
				}
				if (arguments.size() >= 2) {
					secondArgument = (String) arguments.get(1);
				}
				if (JAPAN.equals(firstArgument)) {
					result = requestContext.getWebApplicationContext().getMessage(secondArgument, args.toArray(), Locale.JAPAN);
				}
				if (CHINA.equals(firstArgument)) {
					result = requestContext.getWebApplicationContext().getMessage(secondArgument, args.toArray(), Locale.CHINA);
				}
			} else {
				List<Object> args = new ArrayList<Object>();
				if (arguments.size() > 1) {
					for (int i = 1; i < arguments.size(); i++) {
						args.add(arguments.get(i));
					}
				}
				result = requestContext.getWebApplicationContext().getMessage(firstArgument, args.toArray(), locale);
			}
			return result;
		}
	}

	/**
	 * 自定义theme函数,可通过此扩展的内置函数可获取theme信息
	 * 
	 * @author shaochuan.wangsc
	 * 
	 */
	public static class ThemeFunction implements TemplateMethodModel {
		RequestContext requestContext;

		public ThemeFunction(RequestContext requestContext) {
			this.requestContext = requestContext;
		}

		@SuppressWarnings("unchecked")
		public Object exec(List arguments) throws TemplateModelException {
			String result = "";
			if (arguments == null || arguments.isEmpty() || "".equals(arguments.get(0))) {
				return result;
			}

			String firstArgument = (String) arguments.get(0);
			LocaleContext localeContext = LocaleContextHolder.getLocaleContext();
			Locale locale = localeContext.getLocale();
			if (JAPAN.equals(firstArgument) || CHINA.equals(firstArgument)) {
				List<Object> args = new ArrayList<Object>();
				String secondArgument = "";
				if (arguments.size() > 2) {
					for (int i = 2; i < arguments.size(); i++) {
						args.add(arguments.get(i));
					}
				}
				if (arguments.size() >= 2) {
					secondArgument = (String) arguments.get(1);
				}
				if (JAPAN.equals(firstArgument)) {
					result = requestContext.getTheme().getMessageSource().getMessage(secondArgument, args.toArray(), Locale.JAPAN);
				}
				if (CHINA.equals(firstArgument)) {
					result = requestContext.getTheme().getMessageSource().getMessage(secondArgument, args.toArray(), Locale.CHINA);
				}
			} else {
				List<Object> args = new ArrayList<Object>();
				if (arguments.size() > 1) {
					for (int i = 1; i < arguments.size(); i++) {
						args.add(arguments.get(i));
					}
				}
				result = requestContext.getTheme().getMessageSource().getMessage(firstArgument, args.toArray(), locale);
			}
			return result;
		}
	}
}
